<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请购编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="请购状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择请购状态" clearable>
            <el-option v-for="dict in dict.type.please_buy_status" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>-->
      <el-form-item label="订单编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单日期" prop="ddDate">
        <el-date-picker clearable v-model="queryParams.ddDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="下单日期">
        <el-date-picker v-model="dateRange" style="display: flex;" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="需求日期" prop="subDate">
        <el-date-picker clearable v-model="queryParams.subDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <!--<el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column sortable label="请购编号" align="center" prop="nos" />
      <el-table-column sortable align="center" label="请购来源" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_source" :value="scope.row.source" />
        </template>
      </el-table-column>

      <el-table-column sortable align="center" label="采购类型" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.caigou_type" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column sortable label="订单编号" align="center" prop="code" show-overflow-tooltip />
      <el-table-column sortable label="下单人" align="center" prop="createBy" />
      <el-table-column sortable label="业务员" align="center" prop="userName" />
      <!--  <el-table-column label="工程名称" align="center" prop="" show-overflow-tooltip />-->

      <el-table-column sortable label="客户名称" align="center" prop="customerName" show-overflow-tooltip />
      <!--<el-table-column label="供应商名称" align="center" prop="supplierName"/>-->
      <el-table-column sortable label="下单日期" align="center" prop="ddDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="需求日期" align="center" prop="subDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="交货日期" align="center" prop="" width="180">
          <template slot-scope="scope">
            &lt;!&ndash; <span>{{ parseTime(scope.row., '{y}-{m}-{d}') }}</span> &ndash;&gt;
          </template>
        </el-table-column>-->

      <el-table-column sortable label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column sortable label="" align="center" prop="createTime" show-overflow-tooltip/>-->
      <el-table-column sortable label="请购状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="200" show-overflow-tooltip />

      <el-table-column align="center" label="附件" prop="files" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.files"></file-upload-show>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)">查看明细
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-time" @click="handleShowProcess(scope.row)">查看流转
          </el-button>
          <el-popconfirm :title="`确认一键生成采购单吗？`" @confirm="handleOneKeyToBuy(scope.row)"
                         v-show="ywTypeOneKey.includes(scope.row.type) && scope.row.genBuy==='0'"
                          style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-key">一键采购</el-button>
          </el-popconfirm>
          <!--<el-button v-if="scope.row.status==='4' || scope.row.status==='6'" size="mini" type="text" icon="el-icon-close" @click="submitBuyBohui(scope.row)">
              弃审
            </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-drawer title="流转记录" :visible.sync="processDrawerShow" :with-header="true" size="20%">
      <process-please-buy-timeline :pleaseBuyId="pleaseBuyId"></process-please-buy-timeline>
    </el-drawer>
    <!--<lvDo-form ref="form" @ok="getList"></lvDo-form>-->
    <BuyForm-list ref="buyFormList" @ok="getList"></BuyForm-list>
    <lvdoBohui-jishu-form @ok="getList" ref="bohuiJishuForm"></lvdoBohui-jishu-form>
  </div>
</template>

<script>
import { deleteAction, getAction, getById } from '@/api/manage'
import BuyFormList from '@/views/develop/pleaseBuy/prjForGoodsIn/BuyFormList'
import LvdoBohuiJishuForm from "@/views/develop/pleaseBuy/prjForGoodsIn/LvdoBohuiJishuForm.vue";
import ProcessPleaseBuyTimeline from "@/views/develop/process/processPleaseBuy/ProcessPleaseBuyTimeline.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
export default {
  dicts: ['please_buy_status', 'please_buy_source','caigou_type'],
  name: "LvdoYiTongyiList",
  components: {
    FileUploadShow,
    ProcessPleaseBuyTimeline,
    BuyFormList,
    LvdoBohuiJishuForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单表格数据
      dataList: [],
      dateRange: [],
      ywTypeOneKey:['6','20','21','22','23'],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        money: null,
        counts: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        linkman: null,
        linktel: null,
        idcard: null,
        moneycard: null,
        status: "6",
        orderByColumn: 'a.nos',
        isAsc: 'desc',
      },
      url: {
        list: '/lvDo/lvDo/list',
        remove: '/lvDo/lvDo/',
        export: '/lvDo/lvDo/export',
        oneKeyToBuy: '/lvDo/lvDo/oneKeyToBuy/',
      },
      tableMaxHeight: window.innerHeight - 320,
      baseUrl: process.env.VUE_APP_BASE_API,
      processDrawerShow: false,
      pleaseBuyId: null,
    };
  },
  created() {
    this.queryParams.status = this.$route.query.status;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    submitBuyBohui(row) {
      this.$refs.bohuiJishuForm.showJishuBohui(row);
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    handleShow(row) {
      this.$refs.buyFormList.showDoList(row);
    },
    /** 查询请购单列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange, "ApplyTime");
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.queryParams.status = "4";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `lvDo_${new Date().getTime()}.xlsx`)
    },
    /** 查看流转信息 */
    handleShowProcess(row) {
      this.processDrawerShow = true;
      this.pleaseBuyId = row.id;
    },

    handleOneKeyToBuy(row) {
      getAction(this.url.oneKeyToBuy+row.id, {}).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message);
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      });
    }






  }
};
</script>
